package com.sugar.subscribe.course.module.business.controller.admin.coachcertificate;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.subscribe.course.framework.common.pojo.PageParam;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.common.pojo.CommonResult;
import com.sugar.subscribe.course.framework.common.util.object.BeanUtils;
import static com.sugar.subscribe.course.framework.common.pojo.CommonResult.success;

import com.sugar.subscribe.course.framework.excel.core.util.ExcelUtils;

import com.sugar.subscribe.course.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.subscribe.course.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.subscribe.course.module.business.controller.admin.coachcertificate.vo.*;
import com.sugar.subscribe.course.module.business.dal.dataobject.coachcertificate.CoachCertificateDO;
import com.sugar.subscribe.course.module.business.service.coachcertificate.CoachCertificateService;

@Tag(name = "管理后台 - 教练证书")
@RestController
@RequestMapping("/business/coach-certificate")
@Validated
public class CoachCertificateController {

    @Resource
    private CoachCertificateService coachCertificateService;

    @PostMapping("/create")
    @Operation(summary = "创建教练证书")
    //@PreAuthorize("@ss.hasPermission('business:coach-certificate:create')")
    public CommonResult<Long> createCoachCertificate(@Valid @RequestBody CoachCertificateSaveReqVO createReqVO) {
        return success(coachCertificateService.createCoachCertificate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新教练证书")
    //@PreAuthorize("@ss.hasPermission('business:coach-certificate:update')")
    public CommonResult<Boolean> updateCoachCertificate(@Valid @RequestBody CoachCertificateSaveReqVO updateReqVO) {
        coachCertificateService.updateCoachCertificate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除教练证书")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('business:coach-certificate:delete')")
    public CommonResult<Boolean> deleteCoachCertificate(@RequestParam("id") Long id) {
        coachCertificateService.deleteCoachCertificate(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除教练证书")
    //@PreAuthorize("@ss.hasPermission('business:coach-certificate:delete')")
    public CommonResult<Boolean> deleteCoachCertificateList(@RequestParam("ids") List<Long> ids) {
        coachCertificateService.deleteCoachCertificateListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得教练证书")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('business:coach-certificate:query')")
    public CommonResult<CoachCertificateRespVO> getCoachCertificate(@RequestParam("id") Long id) {
        CoachCertificateDO coachCertificate = coachCertificateService.getCoachCertificate(id);
        return success(BeanUtils.toBean(coachCertificate, CoachCertificateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得教练证书分页")
    //@PreAuthorize("@ss.hasPermission('business:coach-certificate:query')")
    public CommonResult<PageResult<CoachCertificateRespVO>> getCoachCertificatePage(@Valid CoachCertificatePageReqVO pageReqVO) {
        return success(coachCertificateService.getCoachCertificatePage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出教练证书 Excel")
    //@PreAuthorize("@ss.hasPermission('business:coach-certificate:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCoachCertificateExcel(@Valid CoachCertificatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        // 导出 Excel
        ExcelUtils.write(response, "教练证书.xls", "数据", CoachCertificateRespVO.class,
                coachCertificateService.getCoachCertificatePage(pageReqVO).getList());
    }

}